<template>
    <div>
        <div class="clearflex" style="position: fixed;top: 140px;z-index: 100;right: 20px;">
            <el-button @click="authProjectEnter" class="fl-right" size="small" type="primary">确 定</el-button>
        </div>
        <el-tree
                :data="projectTreeData"
                :default-checked-keys="projectTreeIds"
                :props="projectDefaultProps"
                @check="nodeChange"
                default-expand-all
                highlight-current
                node-key="id"
                ref="projectTree"
                show-checkbox
        ></el-tree>
    </div>
</template>
<script>
    import { getProjectList } from '@/api/cmdb_project'
    import { UpdateProjectCasbin, getPolicyProjectByAuthorityId } from '@/api/casbin'
    export default {
        name: 'projectAuth',
        props: {
            row: {
                default: function() {
                    return {}
                },
                type: Object
            }
        },
        data() {
            return {
                projectTreeData: [],
                projectTreeIds: [],
                needConfirm:false,
                projectDefaultProps: {
                    id: 'children',
                    label: 'label'
                },
            }
        },
        methods: {
            nodeChange(){
                this.needConfirm = true
            },
            // 暴露给外层使用的切换拦截统一方法
            enterAndNext(){
                this.authProjectEnter()
            },
            // 关联关系确定
            async authProjectEnter() {
                const checkArr = this.$refs.projectTree.getCheckedNodes(true)
                let projectCasbinInfos = []
                checkArr&&checkArr.map(item=>{
                    let projectCasbinInfo = {
                        project_code:item.id,
                    }
                    projectCasbinInfos.push(projectCasbinInfo)
                })
                const res = await UpdateProjectCasbin({
                    authorityId: this.activeUserId,
                    projectCasbinInfos
                })
                if (res.code == 0) {
                    const authorityIdData = await getPolicyProjectByAuthorityId({
                        authorityId: this.activeUserId
                    })
                    let projectAuthData = {
                        authorityId: this.activeUserId,
                        projectCasbinInfos: authorityIdData.data.projects
                    }
                    localStorage.setItem('projectAuth', JSON.stringify(projectAuthData))
                    this.$message({ type: 'success', message: "项目权限设置成功" })
                }
            },
        },
        async created() {
            getProjectList({'page':1, 'pageSize': 999}).then(response => {
                    // 此处要设置 空
                    this.projectTreeData = []
                    response.data.list.forEach((item) =>{
                        this.projectTreeData.push({id:item.project_code,label:item.project_fullname, children: null})
                    })
                })
            const res = await getPolicyProjectByAuthorityId({
                authorityId: this.row.authorityId
            })
            let projects = res.data.projects
            if(projects) {
                const arr = []
                projects.map(item => {
                    // 防止直接选中父级造成全选
                    arr.push(item.project_code)
                })
                this.projectTreeIds = arr
            }
            this.activeUserId = this.row.authorityId

        }
    }
</script>
<style lang="less">
</style>
